package com.xinyin.aiqinhaiback.controller;

import com.xinyin.aiqinhaiback.common.ResponseResult;
import com.xinyin.aiqinhaiback.model.entity.*;
import com.xinyin.aiqinhaiback.service.InformationService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/information")
public class InformationController {

    @Resource
    InformationService informationService;

    //工艺流程查看指定设备
    @PostMapping("/getDeviceInfo")
    public ResponseResult<List<Map<String, Object>>> getDeviceInfo(@RequestBody DeviceReq req) {
        return informationService.getDeviceInfo(req);
    }

    //确认报警状态
    @PostMapping("/ensureAlarmStatus")
    public ResponseResult ensureAlarmStatus(@RequestBody Alarm alarm) {
        informationService.ensureAlarmStatus(alarm);
        return new ResponseResult();
    }

    //查看报警列表
    @GetMapping("/getAlarmList")
    private ResponseResult<AlarmViewData> getAlarmList (){
        return informationService.getAlarmList();
    }


//    @GetMapping("/testadd")
//    private void testadd (){
//       informationService.testadd();
//    }

    //查看历史信息设备
    @GetMapping("/getHistoryDevice")
    private ResponseResult getHistoryDevice (){
        return informationService.getHistoryDevice();
    }
    ///查看历史信息设备细节
    @PostMapping("/getHistoryDeviceDetail")
    public ResponseResult getHistoryDeviceDetail(@RequestBody HistoryDataQuery  historyDataQuery) {
        return informationService.getHistoryDeviceDetail(historyDataQuery);
    }

    //查询设备历史数据
    @GetMapping("/getDeviceHistoryDataByDate")
    public ResponseResult<List<HistoryResp>> getDeviceHistoryDataByDate(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
                                                                                              @RequestParam(name = "deviceId",required = false) Integer deviceId,
                                                                                              @RequestParam(name = "type",required = false) Integer type) {
        List<HistoryResp> deviceHistoryDataByDate = informationService.getDeviceHistoryDataByDate(date, deviceId, type);
        return new ResponseResult<>(deviceHistoryDataByDate);
    }

    //导出历史数据
    @GetMapping("/exportHistory")
    public void exportHistoryData(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @RequestParam(required = false) Integer deviceId,
            @RequestParam(required = false) Integer type,
            HttpServletResponse response){
        informationService.exportHistoryData(startDate, endDate, deviceId, type,response);
    }
}
